/*
 * @Project Name: shoplook
 * @File Name: ApiCustomerImpl
 * @Package Name: com.sm.shoplook.service.customer.impl
 * @Date: 2018/8/14 10:46
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.service.suit.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.dao.mapper.suit.SuitCommentMapper;
import com.sm.shoplook.dao.mapper.suit.SuitMapper;
import com.sm.shoplook.dao.mapper.suit.SuitScanRecordMapper;
import com.sm.shoplook.domain.suit.Suit;
import com.sm.shoplook.domain.suit.SuitComment;
import com.sm.shoplook.domain.suit.SuitScanRecord;
import com.sm.shoplook.service.suit.ApiSuitService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.discover.req.DisCoverPageSearchVo;
import com.sm.shoplook.vo.suit.front.resp.ApiSuitListRespVo;
import com.sm.shoplook.vo.suit.front.resp.SuitPageRespVo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenpy
 * @desc 会员-前端接口
 * @date 2018/8/14 10:46
 * @see
 */
@Service
public class ApiSuitServiceImpl implements ApiSuitService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiSuitServiceImpl.class);
	@Autowired
	private SuitMapper suitMapper;
	@Autowired
	private SuitCommentMapper suitCommentMapper;
	@Autowired
	private SuitScanRecordMapper suitScanRecordMapper;

	@Override
	public ApiResult<Integer> saveSuit(Suit suit) throws ApiException {
		int i;
		if (null == suit.getId()) {
			// 待审核
			suit.setAuditStatus(0);
			// 草稿状态
			suit.setStatus(2);
			suit.setCreateTime(new Date());
			i = suitMapper.insertSuit(suit);
		} else {
			i = suitMapper.updateByPrimaryKeySelective(suit);
		}
		if (i < 1) {
			throw new ApiException();
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, suit.getId());
	}

	@Override
	public ApiResult<List<ApiSuitListRespVo>> selectCategorySuitByCustomerId(Integer customerId) {
		List<Integer> categorys = suitMapper.selectCategorysByCustomerId(customerId);
		if (CollectionUtils.isEmpty(categorys)) {
			return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
		}
		List<ApiSuitListRespVo> suitRespVos = new ArrayList<>();
		for (Integer category : categorys) {
			Suit suit = new Suit();
			suit.setCustomerId(customerId);
			suit.setCategory(category);
			List<Suit> suits = suitMapper.select(suit);
			ApiSuitListRespVo suitRespVo = new ApiSuitListRespVo();
			suitRespVo.setCategory(category);
			suitRespVo.setSuits(suits);
			suitRespVos.add(suitRespVo);
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, suitRespVos);
	}

	@Override
	public ApiResult<PageInfo<SuitPageRespVo>> selectSuitePage(DisCoverPageSearchVo disCoverPageSearchVo) {
		PageHelper.startPage(disCoverPageSearchVo.getOffset(), disCoverPageSearchVo.getPageSize());
		List<SuitPageRespVo> suits = suitMapper.selectSuitePage(disCoverPageSearchVo);
		if (CollectionUtils.isNotEmpty(suits)) {
			suits.stream().forEach(suitPageRespVo -> {
				SuitComment suitComment = new SuitComment();
				suitComment.setSuitId(suitPageRespVo.getId());
				suitComment.setStatus(1);
				int count = suitCommentMapper.selectCount(suitComment);
				LOGGER.info("count: {}", count);
				suitPageRespVo.setCommentCnt(count);
			});
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, new PageInfo<>(suits));
	}

	@Override
	public ApiResult<Suit> selectSuitById(Integer id) {
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, suitMapper.selectByPrimaryKey(id));
	}

	/**
	 * 会员作品统计
	 * @param customerId
	 * @return
	 */
	@Override
	public ApiResult<Map<String, Object>> statisCustomerSuits(Integer customerId, Integer suitId, Integer status) {
		Map map = suitMapper.statisCustomerSuits(customerId, suitId, status);
		int count = suitScanRecordMapper.selectCollectCount(customerId);
		map.put("myCollectCount", count);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	@Override
	public ApiResult<PageInfo<SuitPageRespVo>> selectAllSuitByCustomerId(DisCoverPageSearchVo disCoverPageSearchVo) {
		PageHelper.startPage(disCoverPageSearchVo.getPageNo(), disCoverPageSearchVo.getPageSize());
		List<SuitPageRespVo> suits = suitMapper.selectSuitePage(disCoverPageSearchVo);
		buildSuitPageInfo(suits);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, new PageInfo<>(suits));
	}

	@Override
	public ApiResult deleteSuit(Integer id) {
		suitMapper.deleteByPrimaryKey(id);
		return new ApiResult(ResultMessage.RESULT_SUCCESS_1);
	}

	@Override
	public ApiResult<List<SuitPageRespVo>> getMyCollect(Integer customerId) {
		SuitScanRecord suitScanRecord = new SuitScanRecord();
		suitScanRecord.setCustomerId(customerId);
		suitScanRecord.setType(1);
		List<SuitScanRecord> suitScanRecords = suitScanRecordMapper.select(suitScanRecord);
		if (CollectionUtils.isEmpty(suitScanRecords)) {
			return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
		}
		List<Integer> list = suitScanRecords.stream().map(value -> value.getSuitId()).collect(Collectors.toList());
		List<SuitPageRespVo> suitPageRespVos = suitMapper.selectSuitsByIds(list);
		buildSuitPageInfo(suitPageRespVos);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, suitPageRespVos);
	}

	private void buildSuitPageInfo(List<SuitPageRespVo> suitPageRespVos) {
		suitPageRespVos.stream().forEach(suitPageRespVo -> {
			SuitComment suitComment = new SuitComment();
			suitComment.setSuitId(suitPageRespVo.getId());
			suitComment.setStatus(1);
			int count = suitCommentMapper.selectCount(suitComment);
			LOGGER.info("count: {}", count);
			suitPageRespVo.setCommentCnt(count);
			Integer auditStatus = suitPageRespVo.getAuditStatus();
			String statusCn = "";
			if (auditStatus == 2) {
                statusCn = "（审核不通过）";
			} else if (auditStatus == 0) {
                statusCn = "（待审核）";
            }
			suitPageRespVo.setStatusCn(statusCn);
		});
	}
}
